package com.ruoyi.oos.web.controller.overrun;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oos.overrun.domain.OverrunRecord;
import com.ruoyi.oos.overrun.domain.Tagcount;
import com.ruoyi.oos.overrun.service.IOverrunRecordService;
import com.ruoyi.oos.tag.service.ITagGroupService;

/**
 * 按测点统计Controller
 * 
 * @author bulang
 * @date 2020-05-13
 */
@Controller
@RequestMapping("/overrun/tagcount")
public class TagCountController extends BaseController
{
    private String prefix = "overrun/tagcount";

    @Autowired
    private IOverrunRecordService overrunRecordService;

    @Autowired
    private ITagGroupService tagGroupService;

    @RequiresPermissions("overrun:tagcount:view")
    @GetMapping()
    public String record(ModelMap map)
    {
	    	OverrunRecord overrunRecord = new OverrunRecord();
	    	overrunRecord.setStartDate(DateUtils.getFirstDayOfMonth());
	    	overrunRecord.setEndDate(DateUtils.getDate());
	  		
	  		map.put("data", overrunRecord);
        return prefix + "/tagcount";
    }

        /**
     * 查询测点统计列表
     */
    @RequiresPermissions("overrun:tagcount:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OverrunRecord overrunRecord)
    {
        // 时间查询条件处理
        String startDate = overrunRecord.getStartDate();
        if (StringUtils.isNotEmpty(startDate)) {
        	overrunRecord.setStartDate(startDate + " 00:00:00");
        }
        String endDate = overrunRecord.getEndDate();
        if (StringUtils.isNotEmpty(endDate)) {
        	overrunRecord.setEndDate(endDate + " 23:59:59");
        }
        if (overrunRecord.getGroupid() != null) {
        	List<Long> list = tagGroupService.selectSubTagGroupById(overrunRecord.getGroupid());
        	overrunRecord.setGroupIds(list);
        }

        startPage();
        
        List<Tagcount> list = overrunRecordService.selectCountTagList(overrunRecord);
        return getDataTable(list);
    }
    
    /**
     * 跳转到统计明细页面
     * @param overrunRecord
     * @param map
     * @return
     */
    @RequiresPermissions("overrun:tagcount:view")
    @GetMapping("/toCountdtl")
    public String toCountdtl(OverrunRecord overrunRecord, ModelMap map)
    {
    		map.put("data", overrunRecord);
        return prefix + "/countdtl";
    }
    
    /**
     * 按测点和轮值的统计明细
     * @param overrunRecord
     * @return
     */
    @RequiresPermissions("overrun:tagcount:list")
    @PostMapping("/countdtl")
    @ResponseBody
    public TableDataInfo countdtl(OverrunRecord overrunRecord)
    {
    	startPage();
      
      // 时间查询条件处理
      String startDate = overrunRecord.getStartDate();
      if (StringUtils.isNotEmpty(startDate)) {
      	overrunRecord.setStartDate(startDate + " 00:00:00");
      }
      String endDate = overrunRecord.getEndDate();
      if (StringUtils.isNotEmpty(endDate)) {
      	overrunRecord.setEndDate(endDate + " 23:59:59");
      }
      List<OverrunRecord> list = overrunRecordService.selectCountDetailList(overrunRecord);
      return getDataTable(list);
    }
    
    /**
     * 导出测点统计列表
     */
    @RequiresPermissions("overrun:tagcount:export")
    @Log(title = "测点统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OverrunRecord overrunRecord)
    {
        List<Tagcount> list = overrunRecordService.selectCountTagList(overrunRecord);
        ExcelUtil<Tagcount> util = new ExcelUtil<Tagcount>(Tagcount.class);
        return util.exportExcel(list, "超限测点统计");
    }
}
